<html>
<head>
<script>

var ws = null;
var msg_id = 0;

function connect() {
    if (ws !== null) return log('already connected');
    ws = new WebSocket('ws://127.0.0.1:8088/WebSocket');
    ws.onopen = function () {
        log('connected');
    };
    ws.onerror = function (error) {
        log(error);
    };
    ws.onmessage = function (e) {
        log('recv: ' + e.data);
    };
    ws.onclose = function () {
        log('disconnected');
        ws = null;
    };
    return false;
}

function disconnect() {
    if (ws === null) return log('already disconnected');
    ws.close();
    return false;
}

function FindObj() {
    if (ws === null) return log('please connect first');
    var p1 = document.getElementById('p1').value;
    document.getElementById('p1').value = "";

    var data = {
        "msg_id": msg_id, 
        "action": "examples.test.FIndObj", 
        "id": p1
        //"property": "i_name",
        //"property_value": "first"
    }

    var jsonData = JSON.stringify(data)
        ws.send(jsonData);
    msg_id = msg_id + 1

        log('FindObj: '+ jsonData);

    return false;
}

function DeleteObj() {
    if (ws === null) return log('please connect first');
    var p1 = document.getElementById('p1').value;
    document.getElementById('p1').value = "";

    var data = {
        "msg_id": msg_id, 
        "action": "examples.test.deleteobj", 
        "id": p1
    }

    var jsonData = JSON.stringify(data)
    ws.send(jsonData);
    msg_id = msg_id + 1

    log('DeleteObj: '+ jsonData);

    return false;
}

function  UpdateObj() {
    if (ws === null) return log('please connect first');
    var p1 = document.getElementById('p1').value;
    var p2 = document.getElementById('p2').value;
    var p3 = document.getElementById('p3').value;
    document.getElementById('p1').value = "";
    document.getElementById('p2').value = "";
    document.getElementById('p3').value = "";

    var data = {
        "msg_id": msg_id, 
        "action": "examples.test.UPdAteOBJ", 
        "id": p1,
        "rawdata": [
        {"i_name": p2}, 
        {"property": p3}
        ],
        "indexes": ["i_name_bk"],
    }

    var jsonData = JSON.stringify(data)
        ws.send(jsonData);
    msg_id = msg_id + 1

        log('UpdateObj: '+ jsonData);

    return false;
}

function SaveObj() {
    if (ws === null) return log('please connect first');
    var p1 = document.getElementById('p1').value;
    var p2 = document.getElementById('p2').value;
    var p3 = document.getElementById('p3').value;
    document.getElementById('p1').value = "";
    document.getElementById('p2').value = "";
    document.getElementById('p3').value = "";

    var data = {
        "msg_id": msg_id, 
        "action": "examples.test.SAVEOBJ", 
        "rawdata": [
        {"key1": p1}, 
        {"property": p2},
        {"i_name": p3}
        ],
            "indexes": ["i_name", "property"]
    }

    var jsonData = JSON.stringify(data)
        ws.send(jsonData);
    msg_id = msg_id + 1

        log('saveobj: ' + jsonData);

    return false;
}

function CreateIndex() {
    if (ws === null) return log('please connect first');
    var p1 = document.getElementById('p1').value;
    document.getElementById('p1').value = "";

    var data = {
        "msg_id": msg_id, 
        "action": "examples.test.creAteindex", 
        "property": p1
    }

    var jsonData = JSON.stringify(data)
        ws.send(jsonData);
    msg_id = msg_id + 1

        log('CreateIndex: ' + jsonData);

    return false;
}

function ShowIndex() {
    if (ws === null) return log('please connect first');

    var data = {
        "msg_id": msg_id, 
        "action": "examples.test.ShowIndex" 
    }

    var jsonData = JSON.stringify(data);
    ws.send(jsonData);
    msg_id = msg_id + 1;

    log('ShowIndex: ' + jsonData);

    return false;
}
function DeleteIndex() {
    if (ws === null) return log('please connect first');
    var p1 = document.getElementById('p1').value;
    document.getElementById('p1').value = "";

    var data = {
        "msg_id": msg_id, 
        "action": "examples.test.DeleteIndex", 
        "property": p1
    }

    var jsonData = JSON.stringify(data);
    ws.send(jsonData);
    msg_id = msg_id + 1;

    log('DeleteIndex: ' + jsonData);

    return false;
}

function log(text) {
    var li = document.createElement('li');
    li.appendChild(document.createTextNode(text));
    document.getElementById('log').appendChild(li);
    return false;
}

</script>

</head>
<body>
<form onsubmit="return SaveObj();">
    <button type="button" onclick="return connect();">
        Connect
    </button>
    <button type="button" onclick="return disconnect();">
        Disconnect
    </button>
    key1: <input id="p1" type="text">
    property2: <input id="p2" type="text">
    property3: <input id="p3" type="text">

    <button type="submit">SaveObj</button>
</form>
<form onsubmit="return FindObj();">
    <button type="submit">FindObj</button>
</form>

<form onsubmit="return DeleteObj();">
    <button type="submit">DeleteObj</button>
</form>

<form onsubmit="return UpdateObj();">
    <button type="submit">UpdateObj</button>
</form>
<form onsubmit="return CreateIndex();">
    <button type="submit">CreateIndex</button>
</form>
<form onsubmit="return DeleteIndex();">
    <button type="submit">DeleteIndex</button>
</form>
<form onsubmit="return ShowIndex();">
    <button type="submit">ShowIndex</button>
</form>

<ol id="log"></ol>
</body>
</html>
